from tkinter import *
from tkinter import filedialog
from tkinter import ttk
from threading import Thread
from mini_tools.cjjtool import *
from mini_tools.myexcel import *
from mini_tools.mytools import *
from sql import sqlseverDB
from config import *
import tkinter.messagebox as messagebox
import openpyxl,time,concurrent.futures,csv

class Admin2Fra(Frame):
    def __init__(self,master=None):
        Frame.__init__(self,master)
        Label(self, text='管理员页面', fg="black", bg="yellow",font=("微软雅黑", 13)).pack(fill=X,padx=2)
        ttk.Separator(self,orient='horizontal').pack(fill=X,pady=2)
        DCALLData(self).pack(fill=X,padx=2,pady=2)


class DCALLData(Frame):

    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.ms = sqlseverDB('{SQL Server}', SQL_SERVER, 'SuMaiTongPol', SQL_USER, SQL_PWD)
        self.bmCount=IntVar()
        self.rbmCount=IntVar()
        self.createWidgets()

    def createWidgets(self):

        f0=Frame(self)
        f0.pack(fill=X)

        f00=Frame(f0)
        f00.pack(fill=X)

        Label(f00, text='选择数据表:').pack(side=LEFT, padx=4, pady=2)
        self.dbnameSelect = ttk.Combobox(f00,state='readonly',width=40)
        self.dbnameSelect['value'] = [v for v in DBOSDICT.keys()]
        self.dbnameSelect.pack(side=LEFT, padx=5, pady=2)
        self.dbnameSelect.current(0)

        self.dcbtn=Button(f00,text='导出完整数据链',command=lambda:Thread(target=self.start_dc).start(),fg='#ffffff',bg='green',width=20)
        self.dcbtn.pack(side=LEFT, padx=2, pady=2)
    
    def start_dc(self):

        self.dcbtn.config(state=DISABLED)
        alldcbs=['HouTai','HouTai_ALG','YNBigData','ShopeeListCount','ShopeeADCost','ALHome','ALOrder',
                 'ALMessage','ALDiscussion','ShopeeOrder','ShopeeStock']
        cur_dbname=DBOSDICT[self.dbnameSelect.get()]
        orderstr=''
        if cur_dbname=='AllShop':
            orderstr=' order by pcount desc'
        if cur_dbname=='HouTai':
            orderstr='order by groupname'
        if cur_dbname=='ShopHome':
            orderstr='order by groupname'
        if cur_dbname=='ALLGoodPro':
            orderstr='order by visits desc'

        sqlstr=f'select top 100 * from [{cur_dbname}] {orderstr}'

        if cur_dbname in alldcbs:
            sqlstr=f'select * from [{cur_dbname}] {orderstr}'

        fqz=self.dbnameSelect.get()
        cur_map=DBO_MAP.get(cur_dbname)
        fname=sqlToExcel(fqz,self.ms,sqlstr,dbpmap=cur_map)

        if fname:
            print(f'成功导出:《{fname}》')
            messagebox.showinfo('提示',f'成功导出:《{fname}》')
        
        else:
            print('导出失败')
        self.dcbtn.config(state=NORMAL)

# import tkinter
# list_win=tkinter.Tk()
# list_win.wm_geometry("800x650")
# list_win.title('ADMIN2')
# Admin2Fra(list_win).pack(fill=X,padx=3,pady=3)
# list_win.mainloop()